package cn.db101.view;

import cn.db101.dao.bookDao;
import cn.db101.model.book;

import javax.swing.*;
import javax.swing.border.EmptyBorder;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

public class admin_borrow_mainFrame {
    JFrame jf = new JFrame();
    private JPanel contentPane;
    private JTextField bookid_textField;
    private JTextField bookname_textField;
    private JTextField bookstatus_textField;
    private JTextField readerid_textField;


    /**
     * Create the frame.
     */
    public void init() {
        jf.setTitle("管理员主界面");

        jf.setIconImage(Toolkit.getDefaultToolkit().getImage(admin_borrow_mainFrame.class.getResource("/icon/Libraries_64px_1176502_easyicon.net.png")));
        jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        jf.setBounds(100, 100, 1278, 805);

        JMenuBar menuBar = new JMenuBar();
        jf.setJMenuBar(menuBar);

        JMenu m1 = new JMenu("主界面");
        m1.setIcon(new ImageIcon(admin_borrow_mainFrame.class.getResource("/icon/线性图书.png")));
        menuBar.add(m1);

        JMenu m2 = new JMenu("图书管理规定");
        m2.setIcon(new ImageIcon(admin_borrow_mainFrame.class.getResource("/icon/线性老师.png")));


        menuBar.add(m2);

        JMenu m3 = new JMenu(" 图书查询");
        m3.setIcon(new ImageIcon(admin_borrow_mainFrame.class.getResource("/icon/线性本子vg.png")));
        menuBar.add(m3);

        JMenu m4 = new JMenu("图书借还");
        m4.setIcon(new ImageIcon(admin_borrow_mainFrame.class.getResource("/icon/线性铅笔.png")));
        menuBar.add(m4);

        JMenu m5 = new JMenu("账户管理");
        m5.setIcon(new ImageIcon(admin_borrow_mainFrame.class.getResource("/icon/线性双指点击.png")));
        menuBar.add(m5);

        JMenu m6 = new JMenu("图书管理");
        m6.setIcon(new ImageIcon(admin_borrow_mainFrame.class.getResource("/icon/线性文件管理员.png")));
        menuBar.add(m6);
        contentPane = new JPanel();
        contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
        contentPane.setLayout(new BorderLayout(0, 0));
        jf.setContentPane(contentPane);

        JLayeredPane layeredPane = new JLayeredPane();
        contentPane.add(layeredPane, BorderLayout.CENTER);

        JLayeredPane layeredPane_1 = new JLayeredPane();
        layeredPane_1.setBounds(405, 212, 523, 313);
        layeredPane.add(layeredPane_1);

        JLabel booknum_label = new JLabel("请输入书号：");
        booknum_label.setFont(new Font("微软雅黑", Font.PLAIN, 17));
        booknum_label.setBounds(59, 48, 116, 18);
        layeredPane_1.add(booknum_label);

        bookid_textField = new JTextField();
        bookid_textField.setColumns(10);
        bookid_textField.setBounds(185, 49, 120, 24);
        layeredPane_1.add(bookid_textField);

        JButton search_Button = new JButton("\u641C\u7D22");
        search_Button.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {

                searchBook(e);

            }
        });
        search_Button.setIcon(new ImageIcon(admin_borrow_mainFrame.class.getResource("/icon/\u5E95\u680F-\u6570\u636E\u67E5\u8BE21.png")));
        search_Button.setForeground(Color.BLACK);
        search_Button.setBackground(Color.WHITE);
        search_Button.setBounds(343, 46, 113, 27);
        layeredPane_1.add(search_Button);

        JLabel bookname_label_1 = new JLabel("书名：");
        bookname_label_1.setFont(new Font("微软雅黑", Font.PLAIN, 17));
        bookname_label_1.setBounds(133, 105, 70, 18);
        layeredPane_1.add(bookname_label_1);

        JLabel author_label_1_1 = new JLabel("读者号：");
        author_label_1_1.setFont(new Font("微软雅黑", Font.PLAIN, 17));
        author_label_1_1.setBounds(118, 197, 70, 18);
        layeredPane_1.add(author_label_1_1);

        JLabel state_label_1_2 = new JLabel("图书状态：");
        state_label_1_2.setFont(new Font("微软雅黑", Font.PLAIN, 17));
        state_label_1_2.setBounds(101, 149, 102, 18);
        layeredPane_1.add(state_label_1_2);

        JButton return_Button = new JButton("还书");
        return_Button.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                returnBook(e);
            }
        });
        return_Button.setIcon(new ImageIcon(admin_borrow_mainFrame.class.getResource("/icon/线性书写.png")));
        return_Button.setForeground(Color.BLACK);
        return_Button.setBackground(Color.WHITE);
        return_Button.setBounds(84, 286, 113, 27);
        layeredPane_1.add(return_Button);

        JButton borrow_Button = new JButton("借阅");
        borrow_Button.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                borrowBook(e);
            }
        });
        borrow_Button.setIcon(new ImageIcon(admin_borrow_mainFrame.class.getResource("/icon/线性书架.png")));
        borrow_Button.setForeground(Color.BLACK);
        borrow_Button.setBackground(Color.WHITE);
        borrow_Button.setBounds(288, 286, 113, 27);
        layeredPane_1.add(borrow_Button);

        bookname_textField = new JTextField();
        bookname_textField.setColumns(10);
        bookname_textField.setBounds(228, 99, 120, 24);
        layeredPane_1.add(bookname_textField);

        bookstatus_textField = new JTextField();
        bookstatus_textField.setColumns(10);
        bookstatus_textField.setBounds(228, 150, 120, 24);
        layeredPane_1.add(bookstatus_textField);

        readerid_textField = new JTextField();
        readerid_textField.setColumns(10);
        readerid_textField.setBounds(228, 198, 120, 24);
        layeredPane_1.add(readerid_textField);

        JLabel label = new JLabel("");
        label.setIcon(new ImageIcon(admin_borrow_mainFrame.class.getResource("/img/borrow.jpg")));
        label.setBounds(0, 0, 1250, 722);
        layeredPane.add(label);

        m1.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {

                closeThis();
                new admin_mainFrame().init();
            }
        });
        m2.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {

                closeThis();
                new admin_regulationFrame().init();
            }
        });
        m3.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {

                closeThis();
                new admin_searchFrame().init();
            }
        });
        m5.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {

                closeThis();
                new admin_account_mainFrame().init();
            }
        });
        m6.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {

                closeThis();
                new bookmanage_mainFrame().init();
            }
        });
        jf.setVisible(true);
        jf.setLocationRelativeTo(null);
    }

    protected void searchBook(ActionEvent e) {

        String book_id = bookid_textField.getText();
        if (book_id == null) {
            JOptionPane.showMessageDialog(null, "请输入对应书籍ID");
            return;
        } else {

            book newbook = new book();
            newbook = new bookDao().bookInfo(book_id);
            if (newbook != null) {
                bookname_textField.setText(newbook.getBook_name());
                if (newbook.getBook_status() == 1) {
                    bookstatus_textField.setText("书已经借出");
                } else {
                    bookstatus_textField.setText("书籍状态正常");
                }
            } else {
                JOptionPane.showMessageDialog(null, "未查询到对应书籍");
                bookname_textField.setText("");
                bookstatus_textField.setText("");
                readerid_textField.setText("");
                return;
            }
        }
    }

    public void closeThis() {
        jf.dispose();
    }

    protected void borrowBook(ActionEvent e) {

        String book_id = bookid_textField.getText();
        String reader_id = readerid_textField.getText();
        if (book_id == null || book_id == "") {
            JOptionPane.showMessageDialog(null, "请输入书号");
            return;
        }
        if (reader_id == null || reader_id == "") {
            JOptionPane.showMessageDialog(null, "请填写读者号");
            return;
        }
        int result = new bookDao().borrowBookDao(book_id, reader_id);
        if (result == 1) {
            JOptionPane.showMessageDialog(null, "借书成功");
            return;
        } else if (result == 0) {
            JOptionPane.showMessageDialog(null, "书籍或者读者不存在");
            return;
        } else if (result == -1) {
            JOptionPane.showMessageDialog(null, "书籍被借出或者不存在");
            return;
        } else if (result == -2) {
            JOptionPane.showMessageDialog(null, "读者可借书数量为0");
            return;
        }


    }

    protected void returnBook(ActionEvent e) {

        String book_id = bookid_textField.getText();
        String reader_id = readerid_textField.getText();
        if (book_id == null || book_id == "") {
            JOptionPane.showMessageDialog(null, "请输入书号");
            return;
        }
        if (reader_id == null || reader_id == "") {
            JOptionPane.showMessageDialog(null, "请填写读者号");
            return;
        }
        int result = new bookDao().returnBookDao(book_id, reader_id);
        if (result == 1) {
            JOptionPane.showMessageDialog(null, "还书成功");
            return;
        } else if (result == 0) {
            JOptionPane.showMessageDialog(null, "书籍或者读者不存在");
            return;
        } else if (result == -1) {
            JOptionPane.showMessageDialog(null, "书籍被借出或者不存在");
            return;
        } else if (result == -2) {
            JOptionPane.showMessageDialog(null, "读者可还书数量已满");
            return;
        }

    }

}
